using System;
using System.Collections.Generic;
using System.Text;
using dotnetCHARTING;
using System.Data;
using System.Drawing;
using System.Web.UI.WebControls;
using System.IO;

namespace hxSec.StaticPage
{
    public class clsRSIChrt
    {
        #region Fields
        // 图形数据源
        private DataSet _data;

        private int _width;         // 图形宽度
        private int _height;        // 图形高度 
        private string _filename;   // 图形文件名称
        private string _stockname;  // 证券代码
        private string _format;     // 图形格式
        private string _type;       // 图形分类

        #endregion

        #region Contructors
        public clsRSIChrt(int iWidth, int iHeight, string strStockName, string strFormat, string strType, DataSet dtData)
        {
            Data = dtData;

            Width = iWidth;
            Height = iHeight;
            StockName = strStockName;
            ImgFormat = strFormat;
            Type = strType;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// 图形数据源
        /// </summary>
        public DataSet Data
        {
            get { return _data; }
            set { _data = value; }
        }

        /// <summary>
        /// 图形宽度
        /// </summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        /// <summary>
        /// 图形高度
        /// </summary>
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }

        /// <summary>
        /// 图形文件名称
        /// </summary>
        public string FileName
        {
            get { return _filename; }
            set { _filename = value; }
        }

        /// <summary>
        /// 客户号
        /// </summary>
        public string StockName
        {
            get { return _stockname; }
            set { _stockname = value; }
        }

        /// <summary>
        /// 图形格式
        /// </summary>
        public string ImgFormat
        {
            get { return _format; }
            set { _format = value; }
        }

        /// <summary>
        /// 图形格式
        /// </summary>
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        #endregion

        #region DrawChart
        public byte[] DrawLineChrt()
        {
            if (Data == null || Data.Tables[0].Rows.Count == 0)
                return null;

            dotnetCHARTING.Chart lineChart = new dotnetCHARTING.Chart();

            // Set the title.
            lineChart.Title = StockName + ": RSI走势图";
            lineChart.LegendBox.Position = LegendBoxPosition.BottomMiddle;
            lineChart.LegendBox.Template = "%PercentOfTotal%icon%name%";
            lineChart.LegendBox.Template = "%icon%name";
            lineChart.LegendBox.ClearColors();
            // Set 3D
            lineChart.Use3D = false;

            // Set the Chart1 Type
            lineChart.Type = ChartType.Combo;

            // Set default transparency
            //lineChart.DefaultSeries.DefaultElement.Transparency = 1;

            lineChart.ChartArea.Background.Color = Color.White;
            lineChart.ChartArea.Shadow.Color = Color.White;
            lineChart.ChartArea.YAxis.ZeroTick.GridLine.Color = Color.White;
            lineChart.YAxis.NumberPercision = 0;
            lineChart.YAxis.FormatString = "0.00";
            lineChart.YAxis.ZeroTick.Line.Color = Color.White;
            lineChart.XAxis.ShowGrid = true;
            lineChart.AutoNameLabels = true;

            // Set show value
            //lineChart.DefaultSeries.DefaultElement.ShowValue = true;
            //lineChart.YAxis.FormatString = "0.0";

            // Set labeling mode.
            //lineChart.PieLabelMode = PieLabelMode.Inside;

            // Set the directory where the images will be stored.
            lineChart.TempDirectory = "";

            // Set he chart size.
            lineChart.Width = Unit.Pixel(Width);
            lineChart.Height = Unit.Pixel(Height);

            // Set background
            //Chart1.ChartArea.Background.Color = Color.White;
            //lineChart.ChartArea.ClearColors();

            // Add the random data.
            lineChart.SeriesCollection.Add(getArrayData());

            Bitmap b = lineChart.GetChartBitmap();
            MemoryStream imgStream = new MemoryStream();

            switch (ImgFormat)
            {
                case "bmp":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Bmp);
                    break;
                case "jpg":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
                case "gif":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Gif);
                    break;
                case "png":
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Png);
                    break;
                default:
                    b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    break;
            }


            return imgStream.ToArray();
        }

        private SeriesCollection getArrayData()
        {
            SeriesCollection SC = new SeriesCollection();

            Series sr = new Series();
            sr.Type = SeriesType.Line;
            sr.Line.Width = 1;
            sr.Name = "RSI1";
            for (int i = 0; i < Data.Tables[0].Rows.Count; i++)
            {               
                Element e1 = new Element();
                e1.Marker.Visible = false;
                e1.Name = Convert.ToDateTime(Data.Tables[0].Rows[i]["DATE"]).ToString("MM-dd");
                e1.YValue = Convert.ToDouble(Data.Tables[0].Rows[i]["RSI1"].ToString());
                sr.Elements.Add(e1);
            }
            SC.Add(sr);

            Series sr1 = new Series();
            sr1.Type = SeriesType.Line;
            sr1.Line.Width = 1;
            sr1.Name = "RSI2";
            for (int j = 0; j < Data.Tables[0].Rows.Count; j++)
            {                
                Element e2 = new Element();
                e2.Marker.Visible = false;
                e2.Name = Convert.ToDateTime(Data.Tables[0].Rows[j]["DATE"]).ToString("MM-dd");
                e2.YValue = Convert.ToDouble(Data.Tables[0].Rows[j]["RSI2"].ToString());
                sr1.Elements.Add(e2);
            }
            SC.Add(sr1);

            Series sr2 = new Series();
            sr2.Type = SeriesType.Line;
            sr2.Line.Width = 1;
            sr2.Name = "RSI3";
            for (int j = 0; j < Data.Tables[0].Rows.Count; j++)
            {               
                Element e2 = new Element();
                e2.Marker.Visible = false;
                e2.Name = Convert.ToDateTime(Data.Tables[0].Rows[j]["DATE"]).ToString("MM-dd");
                e2.YValue = Convert.ToDouble(Data.Tables[0].Rows[j]["RSI3"].ToString());
                sr2.Elements.Add(e2);
            }
            SC.Add(sr2);

            return SC;
        }

        #endregion
    }
}
